#!/usr/bin/env python
# -*- coding: utf-8 -*-


# 智能车联项目的数据是由安装在汽车上的终端进行实时上传的。上传的数据包括速度、里程、油耗、
# 电池电压，电机转速等。特点为数据量大，频率高，后端服务需要对这些数据进行分析，处理，存储并以
# 相应的形式展示在平台。整个过程主要包含以下几个需求
# 1. 建立数据流 Pipeline，规范数据流向，明确流入，分析，输出节点
# 2. 数据流应该具备强大的消费能力，能够承载大量数据，高频率的写入，在消息积压时方便扩容
# 3. 完善性能和可用性的监控指标，对服务做到心中有数，不能断流。
# 4. 车辆每秒钟都会上传行驶数据，数据量极大，产品上要做到对车辆的行驶信息能够做到实时查看，
# 对车辆的历史信息应该保存归档，做为历史 case 追查，模型训练的主要数据
# Create your views here.
import json
from django.http import JsonResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
import pymongo

from application.settings import mongo_cli

from vehicle.models import VehicleInfoModel, VehicleFenceModel, VehicleMoveInfoModel, VehicleFaultInfoModel, \
    VehicleFaultLogModel
from vehicle.serializers import (
    VehicleInfoModelSerializer,
    VehicleFenceModelSerializer,
    VehicleMoveModelSerializer,
    VehicleFaultModelSerializer,
    VehicleFaultLogModelSerializer,
)

from dvadmin.utils.viewset import CustomModelViewSet
from application.settings import redis_cli

class VehicleInfoModelViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = VehicleInfoModel.objects.all()
    serializer_class = VehicleInfoModelSerializer
    # 精确查询
    filter_fields = ['vehicle_type', 'vehicle_status', 'vehicle_motor', 'vehicle_battery']
    # 模糊查询
    search_fields = ['vin']

class VehicleFenceModelViewSet(CustomModelViewSet):
    queryset = VehicleFenceModel.objects.all()
    serializer_class = VehicleFenceModelSerializer
    filter_fields = ['fence_name', 'fence_alarm_type', 'status']
    search_fields = ['fence_name']

class VehicleMoveModelViewSet(CustomModelViewSet):
    queryset = VehicleMoveInfoModel.objects.all()
    serializer_class = VehicleMoveModelSerializer
    filter_fields = ['vehicle_vin']
    search_fields = ['vehicle_vin']

class VehicleFaultModelViewSet(CustomModelViewSet):
    queryset = VehicleFaultInfoModel.objects.all()
    serializer_class = VehicleFaultModelSerializer
    filter_fields = ['fault_type', 'fault_group']
    search_fields = ['fault_type', 'fault_group', 'fault_site', 'fault_value', 'fault_label', 'fault_code', 'is_warn']

class VehicleFaultLogModelViewSet(CustomModelViewSet):
    queryset = VehicleFaultLogModel.objects.all()
    serializer_class = VehicleFaultLogModelSerializer
    filter_fields = ['vin']
    search_fields = ['description', 'modifier', 'dept_belong_id', 'vin', 'fault_code', 'log_status']


import json
import time

import redis
import re
from django.http import JsonResponse, HttpResponse

# r.set())
# print(r.get('we')))
# f"redis://default:redispw@192.168.16.14:55000"
pool = redis.ConnectionPool(host='192.168.16.14', port=55000,password = "redispw",decode_responses=True)
r = redis.Redis(connection_pool=pool)
VEHICLE_DATA_DICT = {
    "vin": "vin",
    "motorCurrent": "电机电流",
    "motorSpeed": "电机转速",
    "motorTemperature": "电机温度",
    "motorTorque": "电机转矩",
    "motorVoltage": "电机电压",
    "gear": "档位",
    "remainingBattery": "剩余电量",
    "brakePedal": "制动踏板行程值",
    "fuelConsumptionRate": "燃料消耗率",
    "resistance": "绝缘电阻",
    "singleBatteryMaxTemperature": "单体电池最高温度",
    "singleBatteryMaxVoltage": "单次最大电压",
    "singleBatteryMinTemperature": "单体电池最低温度",
    "singleBatteryMinVoltage": "单体电池最低电压",
    "totalBatteryCurrent": "动力电池充放电电流",
    "totalBatteryVoltage": "动力电池负载端总电压V3",
}


class qiao(View):
    def get(self, request, pk, ):
        # print(pk)
        common_keys = (
            "latitude", "longitude", "speed", "voltage",
            "singleBatteryMinTemperature")
        data = r.lpop(pk)
        print(type(data))
        real_info = dict()
        if data:
            real_info = json.loads(data)
            print(type(real_info))
            common_info = {k: v for k, v in real_info.items() if k in common_keys}
            real_info = {v: real_info.get(k) for k, v in VEHICLE_DATA_DICT.items()}
            real_info.update(common_info)
        return JsonResponse(dict(code=2000, data=real_info))